#include "stdafx.h"
#include "wf_infodist.h"

#include "pf_snapper.h"



WF_InfoDist::WF_InfoDist(PF_Container& container,
                                     PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Info Dist",
                           container, graphicView) {}


QAction* WF_InfoDist::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Distance Point to Point")
	QAction* action = new QAction(tr("&Distance Point to Point"), NULL);
	//action->zetStatusTip(tr("Measures the distance between two points"));
	action->setIcon(QIcon(":/extui/restricthorizontal.png"));
	return action;
}


void WF_InfoDist::init(int status) {
    PF_ActionInterface::init(status);

}



void WF_InfoDist::trigger() {

    PF_DEBUG->print("PF_ActionInfoDist::trigger()");

    if (point1.valid && point2.valid) {
        double dist = point1.distanceTo(point2);
        QString str;
        str.sprintf("%.6f", dist);
        PF_DIALOGFACTORY->commandMessage(tr("Distance: %1").arg(str));
    }
}



void WF_InfoDist::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionInfoDist::mouseMoveEvent begin");

    if (getStatus()==SetPoint1 ||
            getStatus()==SetPoint2) {

        PF_Vector mouse = snapPoint(e);
        switch (getStatus()) {
        case SetPoint1:
            break;

        case SetPoint2:
            if (point1.valid) {
                point2 = mouse;

                deletePreview();

                preview->addEntity(new PF_Line(preview,
                                               PF_LineAttribute(point1,
                                                           point2)));

                drawPreview();
            }
            break;

        default:
            break;
        }
    }

    PF_DEBUG->print("PF_ActionInfoDist::mouseMoveEvent end");
}



void WF_InfoDist::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_InfoDist::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetPoint1:
        point1 = mouse;
    	graphicView->moveRelativeZero(point1);
        setStatus(SetPoint2);
        break;

    case SetPoint2:
        if (point1.valid) {
            point2 = mouse;
            deletePreview();
    		graphicView->moveRelativeZero(point2);
            trigger();
            setStatus(SetPoint1);
        }
        break;

    default:
        break;
    }
}


void WF_InfoDist::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetPoint1:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify first point of distance"),
            tr("Cancel"));
        break;
    case SetPoint2:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify second point of distance"),
            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_InfoDist::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_InfoDist::updateToolBar() {
    switch (getStatus()) {
    case SetPoint1:
    case SetPoint2:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        break;
    default:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarInfo);
        break;
    }
}


// EOF
